package com.siecom.bank;

import com.siecom.tool.ByteTool;
import com.siecom.tool.StringUtil;

public class BankUtil {




	/**
	 * 测试生成ARQC
	 *
	 * @param
	 * @param ACMainKey
	 *            IC卡返回的55域数据为： 9F26: 81 A9 DC 93 10 F8 88 56
	 *
	 *            授权金额： 000000000000 9F0206
	 *
	 *            其它金额： 000000000000 9F0306
	 *
	 *            国家代码： 0156 9F1A02
	 *
	 *            终端验证： 0000000000 9505
	 *
	 *            货币代码： 0156 5F2A02
	 *
	 *            交易日期： 000000 9A03
	 *
	 *            交易类型： 00 9C01
	 *
	 *            随机数： 00000444 9F3704
	 *
	 *            AIP： 7C00 8202
	 *
	 *            ATC： 03D3 9F3602
	 *
	 *            CVR： 0380A800 9F1013
	 * @throws Exception
	 */

	public static String testGenerateARQC(String cardNo, String panSN, String hexATC, String icDataStr, String ACMainKey) throws Exception {

		String AAC = StringUtil.tagParse("9F26", icDataStr);// 9F26应用密文
		String authorizedAmount = StringUtil.tagParse("9F02", icDataStr);// 授权金额
		String otherAmount = StringUtil.tagParse("9F03", icDataStr);// 其他金额
		String countryCode = StringUtil.tagParse("9F1A", icDataStr);// 国家代码
		String authResult = StringUtil.tagParse("95", icDataStr);// 终端验证
		String transCurrCode = StringUtil.tagParse("5F2A", icDataStr);// 货币代码
		String date = StringUtil.tagParse("9A", icDataStr);// 交易日期
		String transType = StringUtil.tagParse("9C", icDataStr);// 交易类型
		String RADN = StringUtil.tagParse("9F37", icDataStr);// AIP
		String AIP = StringUtil.tagParse("82", icDataStr);// AIP
		String ATC = StringUtil.tagParse("9F36", icDataStr);// ATC
		String CVR = StringUtil.tagParse("9F10", icDataStr).substring(0,8);// CVR

		System.out.println("aac:" + AAC);
		System.out.println("authorizedAmount:" + authorizedAmount);
		System.out.println("otherAmount:" + otherAmount);
		System.out.println("countryCode:" + countryCode);
		System.out.println("authResult:" + authResult);
		System.out.println("transCurrCode:" + transCurrCode);
		System.out.println("date:" + date);
		System.out.println("transType:" + transType);
		System.out.println("RADN:" + RADN);
		System.out.println("AIP:" + AIP);
		System.out.println("ATC:" + ATC);
		System.out.println("CVR:" + CVR);
		String source = authorizedAmount + otherAmount + countryCode
				+ authResult + transCurrCode + date + transType +RADN+ AIP + ATC
				+ CVR;
		System.out.println("source:" + source);

		String result =  BankAuthTest
				.generateARQC(cardNo,panSN,hexATC, ACMainKey,source);

		System.out.println("result:" + result);
		return result;
	}

	public static void generateCVN() throws Exception {
		String result = BankAuthTest.generateCVN("6221234567891234", "0712",
				"111", "0123456789ABCDEFFEDCBA9876543210");

		System.out.println("CVN=" + result);
	}

	public static void generateCVN2() throws Exception {
		String result = BankAuthTest.generateCVN("6221234567891234", "0712",
				"000", "0123456789ABCDEFFEDCBA9876543210");

		System.out.println("CVN2=" + result);
	}

	public static void generateMACTest() throws Exception {
		String result = BankAuthTest.generateMAC("6214618888000002074", "00",
				"0029", "00000000000000000000000000000000",
				"04DA9F790A00299E99DA1521DAA0A3000000000000");

		System.out.println("MAC=" + result);
	}
	/**
	 * ARPC校验运算
	 * @param cardNo
	 * @param panSN
	 * @param hexATC
	 * @param ARQC
	 * @param ARC
	 * @param key
	 * @throws Exception
	 */
	public static void generateARPCTest(String cardNo, String panSN, String hexATC, String ARQC, String ARC, String key) throws Exception {

		String arpc = BankAuthTest.generateARPC(ARQC,
				cardNo, panSN, hexATC,
				key, ARC);

		System.out.println("arpc=" + arpc);
	}

	public static byte[] getMac(byte[] key, byte[] Input) {
		int length = Input.length;
		int x = length % 8;
		// 需要补位的长度
		int addLen = 0;
		if (x != 0) {
			addLen = 8 - length % 8;
		}
		int pos = 0;
		// 原始数据补位后的数据
		byte[] data = new byte[length + addLen];
		System.arraycopy(Input, 0, data, 0, length);
		byte[] oper1 = new byte[8];
		System.arraycopy(data, pos, oper1, 0, 8);
		pos += 8;
		// 8字节异或
		for (int i = 1; i < data.length / 8; i++) {
			byte[] oper2 = new byte[8];
			System.arraycopy(data, pos, oper2, 0, 8);
			byte[] t = ByteTool.bytesXOR(oper1, oper2);
			oper1 = t;
			pos += 8;
		}
		// 将异或运算后的最后8个字节（RESULT BLOCK）转换成16个HEXDECIMAL：
		byte[] resultBlock = ByteTool.byte2hex(oper1).getBytes();
		// 取前8个字节MAK加密
		byte[] front8 = new byte[8];
		System.arraycopy(resultBlock, 0, front8, 0, 8);
		byte[] behind8 = new byte[8];
		System.arraycopy(resultBlock, 8, behind8, 0, 8);
		byte[] desfront8 = EncryptUnit.encryptMode(front8, key, EncryptUnit.Algorithm3DES);
		// 将加密后的结果与后8 个字节异或：
		byte[] resultXOR = ByteTool.bytesXOR(desfront8, behind8);
		// 用异或的结果TEMP BLOCK 再进行一次单倍长密钥算法运算
		byte[] buff = EncryptUnit.encryptMode(resultXOR, key, EncryptUnit.Algorithm3DES);
		// 将运算后的结果（ENC BLOCK2）转换成16 个HEXDECIMAL asc
		byte[] retBuf = new byte[8];
		// 取8个长度字节就是mac值
		System.arraycopy(ByteTool.byte2hex(buff).getBytes(), 0, retBuf, 0, 8);
		return retBuf;
	}


}
